import json
from . import models
from .utilsss.public import jsonData
from django.http import HttpResponse, JsonResponse


def register(request):
    data = json.loads(request.GET.get('data'))
    data = data.get('form')
    user_id = data.get('user_id')
    password = data.get('password')
    user_name = data.get('user_name')
    flag = models.register(user_name, user_id, password)
    data = {
        'code': 2000
    }
    if flag:
        return jsonData(code=40000, message='用户名已存在，请更换用户名！')
    else:
        return jsonData(message="可以注册")


def login(request):
    # data = json.loads(request.body)
    data = json.loads(request.GET.get('data'))
    data = data.get('form')
    user_id = data.get('user_id')
    password = data.get('password')
    flag, user_name, id = models.login(user_id, password)
    if flag:
        res = {
            'user_name': user_name,
            'id': id
        }
        return jsonData(data=res)
    return jsonData(code=40000, message="密码不正确")


def getCourseList(request):
    teacher_id = json.loads(request.GET.get('teacher_id'))
    flag, res = models.getCourseList(teacher_id)
    if flag:
        data = {
            'courselist': res
        }
        return jsonData(data=data)
    return jsonData(code=40000, message="获取失败")


def addCourse(request):
    data = json.loads(request.GET.get('data'))
    teacher_id = data.get('teacher_id')
    data = data.get('ruleForm')
    course_name = data.get('name')
    collage_id = 0
    flag = models.addCourse(course_name, teacher_id, collage_id)
    if flag:
        return jsonData(message="创建成功")
    return jsonData(code=40000, message="创建失败")


def addSingle(request):
    data = json.loads(request.GET.get('data'))
    student_id = data.get('student_id')
    student_name = data.get('student_name')
    course_id = data.get('course_id')
    absent_num = data.get('absent_num')
    homework_score = data.get('homework_score')
    mid_score = data.get('mid_score')
    final_score = data.get('final_score')
    flag = models.addSingle(student_id, student_name, course_id, absent_num, homework_score, mid_score, final_score)
    if flag:
        return jsonData(message="添加成功")
    return jsonData(code=40000, message="添加失败")


def delSingle(request):
    data = json.loads(request.GET.get('data'))
    student_id = data.get('student_id')
    course_id = data.get('course_id')
    flag = models.delSingle(student_id, course_id)
    if flag:
        return jsonData(message="删除成功")
    return jsonData(code=40000, message="删除失败")


def calScore(request):
    data = json.loads(request.GET.get('data'))
    course_id = data.get('course_id')
    data = data.get('formInline')
    mid_weight = data.get('mid_weight')
    final_weight = data.get('final_weight')
    homework_weight = data.get('homework_weight')
    absent_score = data.get('absent_score')
    flag = models.calScore(course_id, mid_weight, final_weight, homework_weight, absent_score)
    if flag:
        return jsonData(message="保存成功")
    return jsonData(code=40000, message="保存失败")


def getStudent(request):
    course_id = json.loads(request.GET.get('course_id'))
    data, flag = models.getStudent(course_id)
    if flag:
        stulist=[]
        for da in data:
            temp = {
                'student_id': da[0],
                'student_name': da[1],
                'mid_score': da[2],
                'final_score': da[3],
                'homework_score':da[4],
                'absent_num':da[5],
                'score':da[6]
            }
            stulist.append(temp)
        res = {
            'studentlist': stulist
        }
        return jsonData(data=res)
    return jsonData(code=40000, message="获取失败")


def calculation(request):
    data = json.loads(request.GET.get('data'))
    course_id = data.get('course_id')
    flag = models.calculation(course_id)
    if flag:
        return jsonData(message="成功")
    return jsonData(code=40000, message="失败")